package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItem;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemList;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * Author: Phani Raja Kumar
 * Date  : Feb 19, 2005
 * Time  : 1:19:54 PM
 */
public class PurchaseOrderLoadPartNumbersCommand extends ChipTrackExtendedAction
{
    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {

        HttpSession mSession 		= request.getSession();
        HashMap errors              = new HashMap();
        HashMap info                = new HashMap();
        String  lineItemIndex       = request.getParameter("lineItemIndex");
        String  refresh             = request.getParameter("refresh");

        PurchaseOrderForm purchaseOrderForm                 = ( PurchaseOrderForm ) form;
        PurchaseOrderLineItemList purchaseOrderLineItemList = null;
        PurchaseOrderLineItem purchaseOrderLineItem         = null ;
        String actionForward 		                        = "Search";
        ArrayList partNumberList							= new ArrayList();
		ArrayList partDescriptionList						= new ArrayList();

		try
        {
            purchaseOrderLineItemList = purchaseOrderForm.getPurchaseOrderLineItemList();
            if( lineItemIndex != null && lineItemIndex.trim().length() > 0 )
            {
                try
                {
                    purchaseOrderLineItem = ( PurchaseOrderLineItem ) purchaseOrderLineItemList.get( Integer.parseInt( lineItemIndex ) );
                }
                catch(NumberFormatException nfe){};

                if( "0".equalsIgnoreCase( refresh ) )
                {
                    purchaseOrderLineItem.setDescription("");
                    purchaseOrderLineItem.setCustomerPartNumber("");
                    purchaseOrderLineItem.setPackageType("");
                    purchaseOrderLineItem.setRequestedDate("");
                    purchaseOrderLineItem.setPromisedDate("");
                    purchaseOrderLineItem.setRequestedQuantity(0);
                    purchaseOrderLineItem.setShippedQty(0);
                    purchaseOrderLineItem.setUnitCost(0.0);
                    purchaseOrderLineItem.setExtendedCost(0.0);
                }

				partNumberList = PurchaseOrderDAO.loadPartNumbersByVendorIdAndLocation( purchaseOrderForm.getVendor_id(), purchaseOrderLineItem.getProcessName() );
                purchaseOrderLineItem.setPartNumbersList( partNumberList );
				partDescriptionList = PurchaseOrderDAO.loadPartDescription( purchaseOrderForm.getVendor_id(), purchaseOrderLineItem.getProcessName() );
				purchaseOrderLineItem.setPartDescriptionList( partDescriptionList );

				if( !(partNumberList != null && partNumberList.isEmpty() == false) && !("".equals( purchaseOrderLineItem.getProcessName().trim())) )
				{
                    errors.put( "No Part Numbers for the Vendor in this process ", null );
				}
            }
            request.setAttribute( "purchaseOrderTypesList", PurchaseOrderDAO.loadPurchaseOrderTypes() );
            request.setAttribute( "processNameList", PurchaseOrderDAO.loadProcessNames( purchaseOrderForm.getVendor_id() ) );
            request.setAttribute( "packageTypeList", PurchaseOrderDAO.loadPackageTypeList() );
            request.setAttribute( "financeReasonCodes", PurchaseOrderDAO.loadFinanceHolds() );
            request.setAttribute( "opsReasonCodes", PurchaseOrderDAO.loadOperationsHolds() );
            request.setAttribute( "poTaxes", PurchaseOrderDAO.loadPOTaxesList() );
            String searchType = request.getParameter("searchType");
            request.setAttribute( "searchType", searchType);

            actionForward = "NewManufacturingPO";
       	}
        catch ( Exception e )
        {
            Logger.error( ERROR_LOADING_PURCHASE_ORDER_LISTS, e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_PURCHASE_ORDER_LISTS );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( ERROR_LOADING_PURCHASE_ORDER_LISTS, e ) );
            actionForward = "Search";
        }
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );

		return mapping.findForward( actionForward );
    }
}
